package com.tc.controller;


import com.tc.domain.Medical_service_items;
import com.tc.dto.Medical_service_itemsDTO;
import com.tc.dto.Medical_service_itemsQueryDTO;
import com.tc.service.IMedical_service_itemsService;
import com.tc.utils.ResultVo;
import com.tc.vo.Medical_service_itemsVO;
import com.tc.vo.PageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/medical_service_items")
@Api(tags = "医疗服务项目管理")
public class Medical_service_itemsController {

    @Autowired
    private IMedical_service_itemsService medical_service_itemsService;

    @ApiOperation("分页查询医疗服务项目")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Medical_service_itemsVO>> page(@RequestBody Medical_service_itemsQueryDTO queryDTO) {
        try {
            // 调用Service层方法进行复杂查询
            PageResultVO<Medical_service_itemsVO> pageVO = medical_service_itemsService.pageQuery(queryDTO);
            return ResultVo.ok(pageVO);
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("根据ID查询医疗服务项目")
    @GetMapping("/{id}")
    public ResultVo<Medical_service_itemsVO> getById(@PathVariable("id") Long id) {
        try {
            // 简单查询直接使用MyBatis-Plus
            Medical_service_items item = medical_service_itemsService.getById(id);
            if (item != null) {
                Medical_service_itemsVO vo = new Medical_service_itemsVO();
                BeanUtils.copyProperties(item, vo);
                return ResultVo.ok(vo);
            } else {
                return ResultVo.fail("医疗服务项目不存在");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("新增医疗服务项目")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody Medical_service_itemsDTO itemDTO) {
        try {
            // 简单的数据保存操作，直接使用MyBatis-Plus
            Medical_service_items item = new Medical_service_items();
            BeanUtils.copyProperties(itemDTO, item);
            
            boolean result = medical_service_itemsService.save(item);
            if (result) {
                return ResultVo.ok(true, "新增医疗服务项目成功");
            } else {
                return ResultVo.fail("新增医疗服务项目失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("修改医疗服务项目")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody Medical_service_itemsDTO itemDTO) {
        try {
            // 判断ID是否存在
            if (itemDTO.getItemId() == null) {
                return ResultVo.fail("医疗服务项目ID不能为空");
            }
            
            // 判断医疗服务项目是否存在
            Medical_service_items existItem = medical_service_itemsService.getById(itemDTO.getItemId());
            if (existItem == null) {
                return ResultVo.fail("医疗服务项目不存在");
            }
            
            // 简单的更新操作，直接使用MyBatis-Plus
            Medical_service_items item = new Medical_service_items();
            BeanUtils.copyProperties(itemDTO, item);
            
            boolean result = medical_service_itemsService.updateById(item);
            if (result) {
                return ResultVo.ok(true, "修改医疗服务项目成功");
            } else {
                return ResultVo.fail("修改医疗服务项目失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("删除医疗服务项目")
    @PostMapping("/delete/{id}")
    public ResultVo<Boolean> delete(@PathVariable("id") Long id) {
        try {
            // 判断医疗服务项目是否存在
            Medical_service_items existItem = medical_service_itemsService.getById(id);
            if (existItem == null) {
                return ResultVo.fail("医疗服务项目不存在");
            }
            
            // 简单的删除操作，直接使用MyBatis-Plus
            boolean result = medical_service_itemsService.removeById(id);
            if (result) {
                return ResultVo.ok(true, "删除医疗服务项目成功");
            } else {
                return ResultVo.fail("删除医疗服务项目失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }
    
    @ApiOperation("批量删除医疗服务项目")
    @PostMapping("/batch/delete")
    public ResultVo<Boolean> batchDelete(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return ResultVo.fail("请选择要删除的医疗服务项目");
            }
            
            // 简单的批量删除操作，直接使用MyBatis-Plus
            boolean result = medical_service_itemsService.removeByIds(ids);
            if (result) {
                return ResultVo.ok(true, "批量删除医疗服务项目成功");
            } else {
                return ResultVo.fail("批量删除医疗服务项目失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }
}

